<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>使用ioctl扫描wifi信号获取信号属性的实例(二) - whowin - 发表我个人原创作品的技术博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="whowin" /><meta name="description" content="使用工具软件扫描 wifi 信号是一件很平常的事情，在知晓 wifi 密码的前提下，通常我们会尽可能地连接信号质量比较好的 wifi 信号，但是如何通过编程来扫描 wifi 信号并获得这些信号的属性(比如信号强度等)，却鲜有文章提及，本文在前面博文的基础上通过实例向读者介绍如何通过编程扫描 wifi 信号，并获得信号的一系列的属性，本文给出了完整的源代码，本文程序在 ubuntu 20.04 下编译测试完成，gcc 版本号 9.4.0；阅读本文并不需要对 IEEE802.11 协议有所了解，但本文实例中大量涉及链表和指针，所以本文可能不适合初学者阅读。
" /><meta name="keywords" content="linux, socket, hugo, dos" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://whowin.gitee.io/post/blog/network/0025-another-wifi-scanner-example/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.e3fea119b1980e848b03dffbeddb11dd0fba483eed0e5f11870fb8e31f145bbd.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="使用ioctl扫描wifi信号获取信号属性的实例(二)" />
<meta property="og:description" content="使用工具软件扫描 wifi 信号是一件很平常的事情，在知晓 wifi 密码的前提下，通常我们会尽可能地连接信号质量比较好的 wifi 信号，但是如何通过编程来扫描 wifi 信号并获得这些信号的属性(比如信号强度等)，却鲜有文章提及，本文在前面博文的基础上通过实例向读者介绍如何通过编程扫描 wifi 信号，并获得信号的一系列的属性，本文给出了完整的源代码，本文程序在 ubuntu 20.04 下编译测试完成，gcc 版本号 9.4.0；阅读本文并不需要对 IEEE802.11 协议有所了解，但本文实例中大量涉及链表和指针，所以本文可能不适合初学者阅读。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://whowin.gitee.io/post/blog/network/0025-another-wifi-scanner-example/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2024-04-11T16:43:29+08:00" />
<meta property="article:modified_time" content="2024-04-11T16:43:29+08:00" />

<meta itemprop="name" content="使用ioctl扫描wifi信号获取信号属性的实例(二)">
<meta itemprop="description" content="使用工具软件扫描 wifi 信号是一件很平常的事情，在知晓 wifi 密码的前提下，通常我们会尽可能地连接信号质量比较好的 wifi 信号，但是如何通过编程来扫描 wifi 信号并获得这些信号的属性(比如信号强度等)，却鲜有文章提及，本文在前面博文的基础上通过实例向读者介绍如何通过编程扫描 wifi 信号，并获得信号的一系列的属性，本文给出了完整的源代码，本文程序在 ubuntu 20.04 下编译测试完成，gcc 版本号 9.4.0；阅读本文并不需要对 IEEE802.11 协议有所了解，但本文实例中大量涉及链表和指针，所以本文可能不适合初学者阅读。"><meta itemprop="datePublished" content="2024-04-11T16:43:29+08:00" />
<meta itemprop="dateModified" content="2024-04-11T16:43:29+08:00" />
<meta itemprop="wordCount" content="6551">
<meta itemprop="keywords" content="Linux,网络编程,802.11,wifi,无线网络,ioctl," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="使用ioctl扫描wifi信号获取信号属性的实例(二)"/>
<meta name="twitter:description" content="使用工具软件扫描 wifi 信号是一件很平常的事情，在知晓 wifi 密码的前提下，通常我们会尽可能地连接信号质量比较好的 wifi 信号，但是如何通过编程来扫描 wifi 信号并获得这些信号的属性(比如信号强度等)，却鲜有文章提及，本文在前面博文的基础上通过实例向读者介绍如何通过编程扫描 wifi 信号，并获得信号的一系列的属性，本文给出了完整的源代码，本文程序在 ubuntu 20.04 下编译测试完成，gcc 版本号 9.4.0；阅读本文并不需要对 IEEE802.11 协议有所了解，但本文实例中大量涉及链表和指针，所以本文可能不适合初学者阅读。"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

  <script async src="/js/busuanzi.pure.mini.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9724909319263152"
     crossorigin="anonymous"></script>


</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">WhoWin</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">文章归档</li>
      </a><a href="/article-categories/categories/">
        <li class="mobile-menu-item">文章分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">文章标签</li>
      </a><a href="/about/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">WhoWin</a>
  
  <div style="position:absolute; left: 80px; top: 75px; color: crimson">
      ———开源和分享是技术发展的源泉和动力；本博客所有文章均为原创
  </div>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">文章归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/article-categories/categories/">文章分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">文章标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">使用ioctl扫描wifi信号获取信号属性的实例(二)</h1>

      <div class="post-meta">
        <span class="post-time"> 2024-04-11 </span>
        <div class="post-category">
            <a href="/categories/linux/"> Linux </a>
            <a href="/categories/c-language/"> C Language </a>
            <a href="/categories/network/"> Network </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1-前言">1 前言</a></li>
        <li><a href="#2-遍历网络设备列表">2 遍历网络设备列表</a></li>
        <li><a href="#3-信号质量信号强度信号噪音的获取">3 信号质量、信号强度、信号噪音的获取</a></li>
        <li><a href="#4-无线信号的工作方式">4 无线信号的工作方式</a></li>
        <li><a href="#5-无线信号支持的传输速率">5 无线信号支持的传输速率</a></li>
        <li><a href="#6-beacon-相关信息">6 beacon 相关信息</a></li>
        <li><a href="#7-information-elements">7 Information Elements</a></li>
        <li><a href="#8-实例">8 实例</a></li>
        <li><a href="#欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>使用工具软件扫描 wifi 信号是一件很平常的事情，在知晓 wifi 密码的前提下，通常我们会尽可能地连接信号质量比较好的 wifi 信号，但是如何通过编程来扫描 wifi 信号并获得这些信号的属性(比如信号强度等)，却鲜有文章提及，本文在前面博文的基础上通过实例向读者介绍如何通过编程扫描 wifi 信号，并获得信号的一系列的属性，本文给出了完整的源代码，本文程序在 ubuntu 20.04 下编译测试完成，gcc 版本号 9.4.0；阅读本文并不需要对 IEEE802.11 协议有所了解，但本文实例中大量涉及链表和指针，所以本文可能不适合初学者阅读。</p>
<h2 id="1-前言">1 前言</h2>
<ul>
<li>在<a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a>专栏里写过一篇wifi信号扫描的文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>，与该文相比本文所附带的实例将可以获取更多的 wifi 属性；</li>
<li>在阅读本文前，请阅读<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>，并请理解范例中的程序，该文中所涉及的概念以及数据结构，本文将不再做介绍；</li>
<li>在<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>中，我们使用 <code>ioctl()</code> 启动了 wifi 信号的扫描，并获取了 wifi 信号的 SSID、MAC地址、工作频率和工作信道，但有一些重要的信号属性并没有获得，比如：信号强度、信号质量、信号噪音以及加密方式等，本文将讨论如何获取这些属性；</li>
<li>本文提供的实例的基本框架与文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>中的基本一致；</li>
<li>本文所提供的实例中并不需要第三方库的支持，所以不需要安装任何其它支持软件和库。</li>
</ul>
<h2 id="2-遍历网络设备列表">2 遍历网络设备列表</h2>
<ul>
<li>
<p>在对无线网卡操作之前，首先要找到无线网卡的设备名，在文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>中，使用 <code>getifaddrs()</code> 找到所有的网络接口，然后再用 <code>ioctl()</code> 的 SIOCGIWNAME 命令从中找到无线网卡；</p>
</li>
<li>
<p>其实我们可以从 <code>/proc/net/dev</code> 中找到所有的网络接口，而不必使用 <code>getifaddrs()</code>；</p>
</li>
<li>
<p>就本文而言，需要知道的就是无线网卡的设备名，我们也可以从文件 <code>/proc/net/wireless</code> 文件中直接获得，这种方法更加简单一些，先来看一下这个文件中有什么内容：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cat /proc/net/wireless 
</span></span><span class="line"><span class="cl">Inter-| sta-|   Quality        |   Discarded packets               | Missed | WE
</span></span><span class="line"><span class="cl">face  | tus | link level noise |  nwid  crypt   frag  retry   misc | beacon | 22
</span></span><span class="line"><span class="cl">wlp1s0: 0000   70. -256.  -256     0      0      0      0      1        0
</span></span><span class="line"><span class="cl">$ 
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>可以看到，这个文件的前两行是标题，从第三行起开始是无线网卡的信息，其中接口名称后面紧跟着 &ldquo;: &ldquo;；</p>
</li>
<li>
<p>这台电脑上只有一个无线网卡，其接口名称为：wlp1s0，下面程序片段从 <code>/proc/net/wireless</code> 中提取出无线接口的名称：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="n">FILE</span> <span class="o">*</span><span class="n">fh</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">char</span> <span class="n">buff</span><span class="p">[</span><span class="mi">1024</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">fh</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="s">&#34;/proc/net/wireless&#34;</span><span class="p">,</span> <span class="s">&#34;r&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="p">(</span><span class="n">fh</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// Skip 2 lines of header
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">fgets</span><span class="p">(</span><span class="n">buff</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">buff</span><span class="p">),</span> <span class="n">fh</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">fgets</span><span class="p">(</span><span class="n">buff</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">buff</span><span class="p">),</span> <span class="n">fh</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// Read each device line
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="k">while</span> <span class="p">(</span><span class="n">fgets</span><span class="p">(</span><span class="n">buff</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">buff</span><span class="p">),</span> <span class="n">fh</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kt">char</span> <span class="n">name</span><span class="p">[</span><span class="n">IFNAMSIZ</span> <span class="o">+</span> <span class="mi">1</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="c1">// Skip empty lines.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="k">if</span> <span class="p">((</span><span class="n">buff</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="sc">&#39;\0&#39;</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="n">buff</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="sc">&#39;\0&#39;</span><span class="p">))</span> <span class="k">continue</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// Extract interface name
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="kt">char</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="n">buff</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// Skip leading spaces
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="k">while</span> <span class="p">(</span><span class="n">isspace</span><span class="p">(</span><span class="o">*</span><span class="n">p</span><span class="p">))</span> <span class="n">p</span><span class="o">++</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="kt">char</span> <span class="o">*</span><span class="n">end</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">end</span> <span class="o">=</span> <span class="n">strstr</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="s">&#34;: &#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// Not found
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="k">if</span> <span class="p">(</span><span class="n">end</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">continue</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// Copy
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="n">memcpy</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="p">(</span><span class="n">end</span> <span class="o">-</span> <span class="n">p</span><span class="p">));</span>
</span></span><span class="line"><span class="cl">        <span class="n">name</span><span class="p">[</span><span class="n">end</span> <span class="o">-</span> <span class="n">p</span><span class="p">]</span> <span class="o">=</span> <span class="sc">&#39;\0&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">printf</span><span class="p">(</span><span class="s">&#34;The wireless interface name is %s</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">name</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="n">fclose</span><span class="p">(</span><span class="n">fh</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;Can&#39;t open file /proc/net/wireless</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<h2 id="3-信号质量信号强度信号噪音的获取">3 信号质量、信号强度、信号噪音的获取</h2>
<ul>
<li>通过阅读文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>，应该可以了解如何使用 <code>ioctl()</code> 启动 wifi 信号的扫描并获得扫描结果，在此简单回顾一下：
<ul>
<li><code>struct iwreq</code> 定义，其中 <code>struct iwreq_data</code> 见下面定义；
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">iwreq</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">union</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kt">char</span>  <span class="n">ifrn_name</span><span class="p">[</span><span class="n">IFNAMSIZ</span><span class="p">];</span>  <span class="cm">/* if name, e.g. &#34;eth0&#34; */</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span> <span class="n">ifr_ifrn</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="cm">/* Data part (defined just above) */</span>
</span></span><span class="line"><span class="cl">    <span class="k">union</span> <span class="n">iwreq_data</span>  <span class="n">u</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li><code>ioctl()</code> 的调用方式：<code>int ioctl(int socket, unsigned long request, struct iwreq *wrq)</code></li>
<li>启动 wifi 扫描时，<code>request=SIOCSIWSCAN</code>，<code>wrq-&gt;ifr_name</code> 设置为无线接口名称，<code>wrq-&gt;u.data.pointer=NULL</code>，<code>wrq-&gt;u.data.flags=0</code>，<code>wrq-&gt;u.data.length=0</code>，然后调用 <code>ioctl()</code>;</li>
<li>获取扫描结果时，<code>request=SIOCGIWSCAN</code>，<code>wrq-&gt;u.data.pointer=接收数据缓冲区指针</code>，<code>wrq-&gt;u.data.length=接收缓冲区的长度</code>，<code>wrq-&gt;u.data.flags=0</code>，然后调用 <code>ioctl()</code>，调用时，<code>wrq-&gt;ifr_name</code> 也是要设置为无线接口名称的，只是因为在启动扫描时已经设置过，所以这里通常不需要再设置；</li>
<li>如果返回的扫描结果数据比较大，设置的接收缓冲区不够用，<code>ioctl()</code> 将返回 -1，errno 为 E2BIG，此时应该重新为缓冲区分配内存并再次调用 <code>ioctl()</code> 获取扫描结果；</li>
<li>如果在使用 <code>ioctl()</code> 获取扫描结果时，扫描还没有完成，<code>ioctl()</code> 将返回 -1，errno 为 EAGAIN，此时应该等待一会再次调用 <code>ioctl()</code> 获取扫描结果；</li>
<li>正常获取扫描结果时，<code>wreq-&gt;u.data.falgs</code> 将被设为 1(调用时为 0)，<code>wreq-&gt;u.data.length</code> 中为返回数据的实际长度，返回的数据被存放在 <code>wreq-&gt;u.data.pointer</code> 指向的数据缓冲区中；</li>
</ul>
</li>
<li>返回的扫描结果是一个数据流(stream)，其中包含着许多的事件(event)，每个 event 包含着一个属性，返回的扫描结果数据符合 <code>struct iw_event</code>，每个 event 数据也符合 <code>struct iw_event</code>，这个结构定义在 <code>wireless.h</code> 中：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">union</span> <span class="n">iwreq_data</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="cm">/* Config - generic */</span>
</span></span><span class="line"><span class="cl">    <span class="kt">char</span>                <span class="n">name</span><span class="p">[</span><span class="n">IFNAMSIZ</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">    <span class="cm">/* Name : used to verify the presence of  wireless extensions.
</span></span></span><span class="line"><span class="cl"><span class="cm">     * Name of the protocol/provider... */</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_point</span>     <span class="n">essid</span><span class="p">;</span>      <span class="cm">/* Extended network name */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">nwid</span><span class="p">;</span>       <span class="cm">/* network id (or domain - the cell) */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_freq</span>      <span class="n">freq</span><span class="p">;</span>       <span class="cm">/* frequency or channel :
</span></span></span><span class="line"><span class="cl"><span class="cm">                                     * 0-1000 = channel
</span></span></span><span class="line"><span class="cl"><span class="cm">                                     * &gt; 1000 = frequency in Hz */</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">sens</span><span class="p">;</span>       <span class="cm">/* signal level threshold */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">bitrate</span><span class="p">;</span>    <span class="cm">/* default bit rate */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">txpower</span><span class="p">;</span>    <span class="cm">/* default transmit power */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">rts</span><span class="p">;</span>        <span class="cm">/* RTS threshold threshold */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">frag</span><span class="p">;</span>       <span class="cm">/* Fragmentation threshold */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u32</span>               <span class="n">mode</span><span class="p">;</span>       <span class="cm">/* Operation mode */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">retry</span><span class="p">;</span>      <span class="cm">/* Retry limits &amp; lifetime */</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_point</span>     <span class="n">encoding</span><span class="p">;</span>   <span class="cm">/* Encoding stuff : tokens */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">power</span><span class="p">;</span>      <span class="cm">/* PM duration/timeout */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_quality</span>   <span class="n">qual</span><span class="p">;</span>       <span class="cm">/* Quality part of statistics */</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">sockaddr</span>     <span class="n">ap_addr</span><span class="p">;</span>    <span class="cm">/* Access point address */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">sockaddr</span>     <span class="n">addr</span><span class="p">;</span>       <span class="cm">/* Destination address (hw/mac) */</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_param</span>     <span class="n">param</span><span class="p">;</span>      <span class="cm">/* Other small parameters */</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">iw_point</span>     <span class="n">data</span><span class="p">;</span>       <span class="cm">/* Other large parameters */</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">iw_event</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u16</span>               <span class="n">len</span><span class="p">;</span>        <span class="cm">/* Real length of this stuff */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u16</span>               <span class="n">cmd</span><span class="p">;</span>        <span class="cm">/* Wireless IOCTL */</span>
</span></span><span class="line"><span class="cl">    <span class="k">union</span> <span class="n">iwreq_data</span>    <span class="n">u</span><span class="p">;</span>          <span class="cm">/* IOCTL fixed payload */</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li><code>struct iw_event</code> 的前两个字段，len 表明了这个 event 的数据长度，cmd 表明了这个 event 的类别，不同的 event，字段 u 中对应的数据结构也不相同；</li>
<li>在文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>中，我们只解析了三个 cmd，SIOCGIWAP(MAC地址)、SIOCGIWESSID(SSID) 和 SIOCGIWFREQ(Frequence 和 Channel)：
<ul>
<li>当 cmd 为 SIOCGIWAP 时，字段 u 对应的数据结构为 <code>struct sockaddr</code>，MAC 地址存放在 <code>u.addr.sa_data</code> 的前 6 个字节中；</li>
<li>当 cmd 为 SIOCGIWESSID 时，字段 u 对应的数据结构为 <code>struct iw_essid</code>，这个是自己定义的，在上面 union 中并没有列出，这个定义可以使问题更加简单一些；</li>
<li>当 cmd 为 SIOCGIWFREQ 时，字段 u 对应的数据结构为 <code>struct iw_freq freq</code>，当计算出的频率大于 1000 时，则结果为 wifi 信号的工作频率，否则为该信号的工作信道；</li>
</ul>
</li>
<li>当 cmd 为 IWEVQUAL，获得的信息为统计数据的信号质量部分(Quality part of statistics)，这部分数据中包括信号质量、信号强度、信号噪音等信息；
<ul>
<li>
<p>此时字段 u 对应的数据结构为 <code>struct iw_quality</code>，在 <code>wireless.h</code> 中定义，如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">iw_quality</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u8</span>        <span class="n">qual</span><span class="p">;</span>       <span class="cm">/* link quality */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u8</span>        <span class="n">level</span><span class="p">;</span>      <span class="cm">/* signal level (dBm) */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u8</span>        <span class="n">noise</span><span class="p">;</span>      <span class="cm">/* noise level (dBm) */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u8</span>        <span class="n">updated</span><span class="p">;</span>    <span class="cm">/* Flags to know if updated */</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>qual 字段为信号连接质量，先看一下使用无线工具 <code>sudo iwlist [wifname] scan</code> 扫描信号看到的信号连接质量是什么样子的，[wifname] 是无线网络接口的名称，在我的电脑上是 <code>wlp1s0</code>，不同电脑可能会不一样；</p>
<p><img src="https://whowin.gitee.io/images/180025/screenshot-of-iwlist-scan.png" alt="Screenshot of wifi scanning"></p>
</li>
<li>
<p>图中红线所示部分就是信号连接质量，其表达方式为 <code>47/70</code>，这是什么含义呢？</p>
</li>
<li>
<p>WE(Wireless Extension) 假设信号范围为 <code>-110dBm ~~ -40dBm</code>，信号质量的值为信号强度 +110 得出的，这样信号质量值的范围为 <code>0~~70</code>，<code>47/70</code> 的 47 表示信号连接质量值为 47，70 标志信号质量值最大为 70；</p>
</li>
<li>
<p>level 字段为信号的强度，其单位为 dBm(分贝毫瓦)，通常用于表示无线电信号的功率，如上所述，正常情况下，<code>level + 110 = qual</code></p>
</li>
<li>
<p>noise 字段为信号背景噪音的强度，这个字段在我的电脑上并不支持，如何判断是否支持，请看下面对 updated 字段的介绍；</p>
</li>
<li>
<p>updated 字段是一个位掩码(bit mask)，在 <code>wireless.h</code> 中定义了该字段每个 bit 表达的含义，如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cm">/* Statistics flags (bitmask in updated) */</span>
</span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_QUAL_UPDATED    0x01    </span><span class="cm">/* Value was updated since last read */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_LEVEL_UPDATED   0x02
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_NOISE_UPDATED   0x04
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_ALL_UPDATED     0x07
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_DBM             0x08    </span><span class="cm">/* Level + Noise are dBm */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_QUAL_INVALID    0x10    </span><span class="cm">/* Driver doesn&#39;t provide value */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_LEVEL_INVALID   0x20
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_NOISE_INVALID   0x40
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_RCPI            0x80    </span><span class="cm">/* Level + Noise are 802.11k RCPI */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_QUAL_ALL_INVALID     0x70
</span></span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>如果网卡驱动程序不支持 quality、level 或者 noise，则 IW_QUAL_QUAL_INVALID、IW_QUAL_LEVEL_INVALID 或者 IW_QUAL_NOISE_INVALID 对应的 bit 就会被置 1；</p>
</li>
<li>
<p>如果自上次读取 quality、level 或者 noise 后，数据已经被网卡驱动程序再次更新，则 IW_QUAL_QUAL_UPDATED、IW_QUAL_LEVEL_UPDATED 或者 IW_QUAL_NOISE_UPDATED 对应的 bit 会被置 1；</p>
</li>
</ul>
</li>
</ul>
<h2 id="4-无线信号的工作方式">4 无线信号的工作方式</h2>
<ul>
<li>wireless.h 中定义了 8 中无线信号的工作方式：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cm">/* Modes of operation */</span>
</span></span><span class="line"><span class="cl"><span class="cp">#define IW_MODE_AUTO        0   </span><span class="cm">/* Let the driver decides */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_MODE_ADHOC       1   </span><span class="cm">/* Single cell network */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_MODE_INFRA       2   </span><span class="cm">/* Multi cell network, roaming, ... */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_MODE_MASTER      3   </span><span class="cm">/* Synchronisation master or Access Point */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_MODE_REPEAT      4   </span><span class="cm">/* Wireless Repeater (forwarder) */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_MODE_SECOND      5   </span><span class="cm">/* Secondary master/repeater (backup) */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_MODE_MONITOR     6   </span><span class="cm">/* Passive monitor (listen only) */</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#define IW_MODE_MESH        7   </span><span class="cm">/* Mesh (IEEE 802.11s) network */</span><span class="cp">
</span></span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>我们扫描到的信号，大多数应该是 Master；</li>
<li>当使用 <code>ioctl()</code> 扫描无线信号时，返回的 <code>struct iw_event</code> 中当 cmd 字段为 SIOCGIWMODE，该事件为工作方式；</li>
<li>当 cmd 为 SIOCGIWMODE时，<code>struct iw_event</code> 中的 <code>u.mode</code> 为该无线信号的工作方式；</li>
</ul>
<h2 id="5-无线信号支持的传输速率">5 无线信号支持的传输速率</h2>
<ul>
<li>当使用 <code>ioctl()</code> 扫描无线信号时，返回的 <code>struct iw_event</code> 中当 cmd 字段为 SIOCGIWRATE 时，该事件中的数据为信号支持的传输速率；</li>
<li>一个信号支持的传输速率通常有很多种，所以这个数据通常也是有很多组的，下面是一组实际的数据(16进制数)：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">0000:   28 00 21 8B 00 00 00 00 80 8D 5B 00 00 00 00 00 
</span></span><span class="line"><span class="cl">0010:   00 1B B7 00 00 00 00 00 00 36 6E 01 00 00 00 00 
</span></span><span class="line"><span class="cl">0020:   00 6C DC 02 00 00 00 00
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>按照 <code>struct iw_event</code> 的定义，前两个字节是这个 event 的长度，为 0x0028，也就是 40 个字节，后面两个字节 0x8B21 是 cmd 字段，0x8b21 也就是 SIOCGIWRATE(见 <code>wireless.h</code> 中的定义)，所以这个 event 的数据是传输速率；</li>
<li>当收到的是传输速率时，<code>struct iw_event</code> 中的 <code>u.bitrate</code> 为对应的传输率的数据结构(见第 3 节关于 <code>union iwreq_data</code> 的介绍)，<code>u.bitrate</code> 是一个 <code>struct iw_param</code>，其定义如下(见 wireless.h)：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">iw_param</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">__s32</span>   <span class="n">value</span><span class="p">;</span>      <span class="cm">/* The value of the parameter itself */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u8</span>    <span class="n">fixed</span><span class="p">;</span>      <span class="cm">/* Hardware should not use auto select */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u8</span>    <span class="n">disabled</span><span class="p">;</span>   <span class="cm">/* Disable the feature */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__u16</span>   <span class="n">flags</span><span class="p">;</span>      <span class="cm">/* Various specifc flags (if any) */</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>根据其定义，其中的 <code>u.bitrate.value</code> 字段即为传输速率；</li>
<li>如上数据，一个 wifi 信号通常都是支持多种传输速率的，这时可以将数据部分定义成一个 <code>struct iw_param</code> 的结构数组，并通过 event 的长度和 <code>struct iw_param</code> 的长度计算得出这个 event 中有多少组传输速率的数据，如下：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="p">......</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">iw_event</span> <span class="o">*</span><span class="n">evp</span> <span class="o">=</span> <span class="n">data</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">rate_count</span> <span class="o">=</span> <span class="p">(</span><span class="n">evp</span><span class="p">.</span><span class="n">len</span> <span class="o">-</span> <span class="n">IW_EV_LCP_LEN</span><span class="p">)</span> <span class="o">/</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">iw_param</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">iw_param</span> <span class="o">*</span><span class="n">rates</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">evp</span><span class="o">-&gt;</span><span class="n">u</span><span class="p">.</span><span class="n">bitrate</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">rate_count</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">......</span>
</span></span><span class="line"><span class="cl">    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;Bit rate: %d Mb/s</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">rates</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">value</span> <span class="o">/</span> <span class="mi">1000000</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>其中 IW_EV_LCP_LEN 为 <code>struct iw_event</code> 中结构头(len 和 cmd 字段)长度(包含为对齐而填充的空字符)，请见文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>中的相关解释；</li>
<li>当一个 WiFi 信号支持的传输速率比较多时，可能会收到两个 SIOCGIWRATE 事件(也许会有多个，但我没有遇到过)，每个事件中的速率是不一样的，所以都需要处理，不能忽略任何一个事件；</li>
</ul>
<h2 id="6-beacon-相关信息">6 beacon 相关信息</h2>
<ul>
<li>
<p>当使用 <code>ioctl()</code> 扫描无线信号时，返回的 <code>struct iw_event</code> 中当 cmd 字段为 IWEVCUSTOM 时，该事件在 <code>wireless.h</code> 中定义为 &ldquo;Driver specific ascii string&rdquo;，意为：驱动程序特定的 ASCII 字符串；</p>
</li>
<li>
<p>AP 要周期性地在 wifi 上广播 beacon 帧，用于在网络上宣告一个 wifi 信号的存在，之所以可以扫描到 wifi 信号就是因为收到了 beacon 帧；</p>
</li>
<li>
<p>beacon 帧并不是本文要讨论的问题，本文不会展开讨论；</p>
</li>
<li>
<p>回到 WiFi 信号的扫描主题上，<code>wireless.h</code> 中并没有定义一个事件可以收到有关 beacon 帧的信息，但是我们在事件 IWEVCUSTOM 中看到了 beacon 信息；</p>
</li>
<li>
<p>IWEVCUSTOM 事件中的这个字符串的结构与 essid 是一样的，所以可以用相同的方法提取，可以参考文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>；</p>
</li>
<li>
<p>有意思的是起初并不知道这个字符串中会有什么内容，但把收到的内容显示出来后，发现是类似下面的内容：</p>
<p><img src="https://whowin.gitee.io/images/180025/screenshot-of-driver-string.png" alt="Screenshot of driver specific ascii string"></p>
</li>
<li>
<p>图中红线所示就是收到的字符串，原来这个字符串中藏着与 beacon 帧有关的信息，Last beacon 是最后收到的 beacon 帧时间，TSF 是 Timing Synchronization Function 的缩写，是 beacon 帧中的一个字段，用于 AP 和 Station 之间同步时间；</p>
</li>
<li>
<p>这个事件与 essid 还是有所不同，一个 WiFi 信号只有一个 essid，所以 SIOCGIWESSID 事件只会收到一次，但是 IWEVCUSTOM 有可能收到多次，而每次收到的字符串是不相同的，所以接收 IWEVCUSTOM 事件比接收 essid 还是要麻烦一些，在本文实例源程序中这部分有详细的中文注释；</p>
</li>
</ul>
<h2 id="7-information-elements">7 Information Elements</h2>
<ul>
<li>
<p>当使用 <code>ioctl()</code> 扫描无线信号时，返回的 <code>struct iw_event</code> 中当 cmd 字段为 IWEVGENIE 时，该事件在 <code>wireless.h</code> 中定义为 &ldquo;Generic IE (WPA, RSN, WMM, ..)&quot;，意为通用 IE(Information Elements)；</p>
</li>
<li>
<p>IE 可以提供非常多的信息，本文中仅就 SSID 和速率信息进行示范性的解析；</p>
</li>
<li>
<p>首先，这个事件在大多数情况下都是提供多个 IE，所以事件通常比较长，而且长度并不固定；</p>
</li>
<li>
<p>我们先来看一个实际收到的 IE 数据</p>
<p><img src="https://whowin.gitee.io/images/180025/screenshot-of-IE-data.png" alt="Real IE data"></p>
</li>
<li>
<p>数据与普通事件一样，符合 <code>struct iw_event</code> 结构，前两个字节是事件数据的长度，0x00D2(十进制 210)字节，第 3、4 字节为事件类型，0x8C05(IWEVGENIE) 表示这个事件中是 IE 数据；</p>
</li>
<li>
<p>前四个字节组成了 <code>struct iw_event</code> 的头信息，紧跟着的 4 个字节为按 64 位对齐填充的字符，第 9、10 字节为实际 IE 数据所占的长度 ie_length，第 11-16 字节是对齐填充字节；</p>
</li>
<li>
<p>所以从第 17 个字节(第 2 行)开始才是真正的 IE 数据，正是因为这个原因，通常 ie_length(第 9、10 字节) 比事件的总长度(第 1、2 字节)小 16(0x10)，在这组实际数据中，事件长度为 0x00D2(十进制 210)，而 IE 数据的长度为 0x00C2(十进制 194)，要注意的是，事件长度是包含 <code>struct iw_event</code> 头信息的 8 个字节，而 IE 数据长度是不包括头信息的长度(仅为 IE 数据长度)；</p>
</li>
<li>
<p>IEEE 标准 <code>802.11-2007</code> 文档中定义了 IE 的结构，该文档的下载地址如下：</p>
<ul>
<li><a href="https://people.iith.ac.in/tbr/teaching/docs/802.11-2007.pdf">Wireless LAN Medium Access Control (MAC) and Physical Layer (PHY) Specifications</a></li>
</ul>
</li>
<li>
<p>该标准的 <code>7.3.2 Information elements</code> 定义了 IE 的结构：</p>
<p><img src="https://whowin.gitee.io/images/180025/screenshot-of-IE-structure.png" alt="IE Structure"></p>
</li>
<li>
<p>每个 IE 符合 <code>type-length-value</code> 格式，即：第 1 个字节表示 IE 类型 Element ID，第 2 个字节表示数据的长度 Length，后面若干字节为实际数据 Information，数据长度为 Length；</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">ieee80211_ie</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">eid</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">len</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>当收到一个 IE 数据的事件时，可以考虑如下定义：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">iw_event_ie</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">short</span> <span class="n">len</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">short</span> <span class="n">cmd</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="cp">#ifdef __x86_64__           </span><span class="c1">// 64位系统按8字节对齐
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">unsigned</span> <span class="kt">short</span> <span class="nf">__attribute</span><span class="p">((</span><span class="n">aligned</span><span class="p">(</span><span class="mi">8</span><span class="p">)))</span><span class="n">ie_len</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">ieee80211_ie</span> <span class="nf">__attribute</span><span class="p">((</span><span class="n">aligned</span><span class="p">(</span><span class="mi">8</span><span class="p">)))</span><span class="n">ie</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="cp">#else                       </span><span class="c1">// 32位系统按4字节对齐
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">unsigned</span> <span class="kt">short</span> <span class="nf">__attribute</span><span class="p">((</span><span class="n">aligned</span><span class="p">(</span><span class="mi">4</span><span class="p">)))</span><span class="n">ie_len</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">ieee80211_ie</span> <span class="nf">__attribute</span><span class="p">((</span><span class="n">aligned</span><span class="p">(</span><span class="mi">4</span><span class="p">)))</span><span class="n">ie</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="cp">#endif
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="p">};</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>字段 len 和 cmd 与 <code>struct iw_event</code> 是一致的，ie_len 字段将得到 ie 这个字段所对应的数据的总长度，<code>struct ieee80211_ie</code> 则定义了每个 IE 的结构，具体有多少个 IE 则需要在遍历 IE 时根据 ie_len 字段的值做出判断；</p>
</li>
<li>
<p><code>struct ieee80211_ie</code> 中的 eid(Element ID) 字段定义了这个 IE 的类型，这些类型在 <code>802.11-2007</code> 的文档中第 100 页有定义，这里摘录其中的一部分：</p>
<p><img src="https://whowin.gitee.io/images/180025/screenshot-of-80211-2007-1.png" alt="Parts of IE type definition"></p>
</li>
<li>
<p>从上面定义可以看到，当 Element ID 为 0 时，其信息内容为 SSID，以上面的实际数据为例，数据的第二行，也就是第 1 个 IE 的数据为：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-plain" data-lang="plain"><span class="line"><span class="cl">00 07 31 35 2D 31 31 30 31 
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>按照 IE 的格式定义，Element ID 为 0，表示其信息为 SSID，数据长度 Length 为 7，所以后面的 7 个字节 <code>31 35 2D 31 31 30 31</code> 为实际数据；</li>
<li>查 ASCII 表，这个数据其实就是 &ldquo;15-1101&rdquo;，这就是这个信号的 SSID</li>
</ul>
</li>
<li>
<p>IE 提供了一种非常灵活的传递信息的方法，内容非常丰富，在本文所载实例中仅就其中的几个进行了解析；</p>
</li>
<li>
<p>还要简单介绍一下所支持传输速率的 IE，因为在实例中解析了这个 IE;</p>
</li>
<li>
<p>根据 Element ID 的定义，当 Element ID 为 1 时，IE 的数据为该信号所支持的传输速率，其结构在 <code>802.11-2007</code> 文档的第 102 页有介绍：</p>
<p><img src="https://whowin.gitee.io/images/180025/screenshot-of-rates.png" alt="Supported Rates"></p>
</li>
<li>
<p>根据文档，一个 IE 中最多描述 8 个传输速率，单位为 500 kb/s，每个速率占用 1 个字节，其最高位(bit 7)有其它意义，(bit 0 - 6) 表示速率值，所以在计算时要将 bit 7 过滤掉；</p>
</li>
<li>
<p>IE 信息会有空信息，也就是长度字段为 0，这种 IE 通常只有 2 个字节，没有意义，在实际解析中要过滤掉，比如：
<code>00 00</code></p>
</li>
</ul>
<h2 id="8-实例">8 实例</h2>
<ul>
<li>
<p>完整的源代码，文件名：<a href="https://gitee.com/whowin/whowin/blob/blog/sourcecodes/180025/wifi-new-scanner.c">wifi-new-scanner.c</a>(<strong>点击文件名下载源程序</strong>)，请务必使用 UTF-8 字符集，否则源程序中的中文注释为乱码；</p>
</li>
<li>
<p>阅读这个源码最好先阅读文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>，并搞懂其中的源代码；</p>
</li>
<li>
<p>简述一下程序的基本流程：</p>
<ul>
<li>通过读取文件 <code>/proc/net/wireless</code> 获取无线网络接口的列表(在文章<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>中使用的是另一种方法)；</li>
<li>使用 <code>ioctl()</code> 向无线网络接口发出 wifi 信号扫描指令；</li>
<li>使用 <code>ioctl()</code> 获取扫描结果，根据返回的结果生成事件链表；</li>
<li>从事件链表中分析每一个事件，从中解析出每个信号的属性，生成 ap 链表；</li>
<li>如果有 IE 事件，还要在 AP 链表中生成 IE 链表；</li>
<li>从 AP 链表中解析出信息并显示出来；</li>
</ul>
</li>
<li>
<p>比较<a href="https://blog.csdn.net/whowin/article/details/131504380">《使用ioctl扫描wifi信号获取信号属性的一个范例(一)》</a>中的实例，本文除了解析出 MAC 地址、ESSID、工作频率、工作信道外，还可以解析出信号质量、工作模式、信号支持的传输速率、驱动程序字符串以及 Information Elements；</p>
</li>
<li>
<p>源程序中有比较详细的注释请自行参考；</p>
</li>
<li>
<p>其中比较复杂的是 IE 的解析，IE 的内容极其丰富，作为示范，本例仅解析了 SSID 和速率，需要更多信息的读者可以查阅 <a href="https://people.iith.ac.in/tbr/teaching/docs/802.11-2007.pdf">802.11-2007</a> 文档；</p>
</li>
<li>
<p>编译：<code>gcc -Wall wifi-new-scanner.c -o wifi-new-scanner -lm</code></p>
</li>
<li>
<p>运行：<code>sudo ./wifi-new-scanner</code></p>
</li>
<li>
<p>运行截图：</p>
<p><img src="https://whowin.gitee.io/images/180025/wifi-new-scanner.gif" alt="GIF of running wifi-new0scanner"></p>
</li>
</ul>
<h2 id="欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></h2>
<hr>
<p><strong>欢迎访问我的博客：https://whowin.cn</strong></p>
<p><strong>email: <a href="mailto:hengch@163.com">hengch@163.com</a></strong></p>
<p><img src="https://whowin.gitee.io/images/qrcode/sponsor-qrcode.png" alt="donation"></p>
<!-- CSDN
[img01]: https://img-blog.csdnimg.cn/img_convert/7d6fc4505697687bb568f4dea1e7f7e3.png
[img02]: https://img-blog.csdnimg.cn/img_convert/3052fde5a47530e97638609e2f6cfebf.png
[img03]: https://img-blog.csdnimg.cn/img_convert/20a274b8e264f90ccf902ce54283a7b3.png
[img04]: https://img-blog.csdnimg.cn/img_convert/36acac0e26b0c21a42747c47d1ab4fcb.png
[img05]: https://img-blog.csdnimg.cn/img_convert/dc8030541ffad49b50e9d338c4893f84.png
[img06]: https://img-blog.csdnimg.cn/img_convert/36a9a890228b3fc5597b57b2ee32b430.png
[img07]: https://img-blog.csdnimg.cn/img_convert/5a5bd5b1aa4d9ac0912e3b89efc43d89.gif
-->
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">whowin</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2024-04-11
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/linux/">Linux</a>
          <a href="/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a>
          <a href="/tags/802.11/">802.11</a>
          <a href="/tags/wifi/">wifi</a>
          <a href="/tags/%E6%97%A0%E7%BA%BF%E7%BD%91%E7%BB%9C/">无线网络</a>
          <a href="/tags/ioctl/">ioctl</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/blog/network/0027-libiw-functions/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">libiw中的函数说明</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/blog/network/0024-using-epoll-in-socket-programming/">
            <span class="next-text nav-default">使用epoll()进行socket编程处理多客户连接的TCP服务器实例</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/blog/network/0025-another-wifi-scanner-example/" class="leancloud_visitors" data-flag-title="使用ioctl扫描wifi信号获取信号属性的实例(二)">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'OFCGzCfJRUglzOdzrqMGkbTR-gzGzoHsz',
        appKey: 'v7P29kPAEbsmaavaYPNhGhnF',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:hengch@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://whowin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>
<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2022 - 
    2024<span class="heart"><i class="iconfont icon-heart"></i></span><span>whowin</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>








</body>
</html>
